package com.globallogic.oopbasics.competence;

import java.io.FileNotFoundException;
import java.util.List;

import org.apache.log4j.Logger;

import com.globallogic.oopbasics.competence.entity.SpecialistProfile;
import com.globallogic.oopbasics.competence.entity.TechCompetency;
import com.globallogic.oopbasics.competence.entity.creator.SpecialistProfileCreator;
import com.globallogic.oopbasics.competence.exception.LevelException;
import com.globallogic.oopbasics.competence.generic.CompetenceBoxCreator;
import com.globallogic.oopbasics.competence.generic.SpecialityProfilesPrinter;

/**
 * @author olena.strelchuk
 */

/**
 * Creates Competence Profiles for different Specialists
 */
public class CompetenceApplication {

	private static final int HIGH_PRIORITY_TO = 1;
	private static final double HIGH_PRIORITY_FROM = 0.3;
	private static final double MEDIUM_PRIORITY_TO = 0.2999;
	private static final double MEDIUM_PRIORITY_FROM = 0.05;
	private static final double LOW_PRIORITY_TO = 0.0499;

	// Logger
	private static Logger log = Logger.getLogger(CompetenceApplication.class);

	private static void getPriorityCompetencies(
			List<SpecialistProfile> spesialistProfiles, double valueFrom,
			double valueTo) {

		for (int i = 0; i < spesialistProfiles.size(); i++) {
			System.out.println(spesialistProfiles.get(i).getSpecialityProfile()
					.getTotalCompetence().getCompetencies(valueFrom, valueTo));
		}
	}

	private static void printCompetencyValues(
			List<SpecialistProfile> spesialistProfiles) {
		for (int i = 0; i < spesialistProfiles.size(); i++) {
			System.out.println("==="
					+ spesialistProfiles.get(i).getSpecialist() + "===");

			System.out.println("==="
					+ spesialistProfiles.get(i).getSpecialityProfile()
							.getSpeciality().getName() + "===");
			for (int j = 0; j < (spesialistProfiles.get(i)
					.getCompetencyValues().length); j++) {

				System.out.println(spesialistProfiles.get(i)
						.getCompetencyValues()[j].toString());
			}
		}

	}

	public static void main(String[] args) throws LevelException,
			FileNotFoundException, IllegalArgumentException {

		try {
			log.info("Start process...");

			// Create new Specialist
			SpecialistProfileCreator specialistProfile = new SpecialistProfileCreator();
			SpecialityProfilesPrinter sp = new SpecialityProfilesPrinter();

			/*
			 * Here we see our initial TotalCompetence Profile as it was
			 * created:
			 */
			System.out.println("1 - INITIAL COMPETENCY PROFILE");
			System.out.println("==============================");
			log.info(sp.getAllSpecialityProfilesString(specialistProfile
					.getSpecialityProfiles()));

			System.out.println("2 - SORT COMPETENCY PROFILE");
			System.out.println("==============================");
			for (int i = 0; i < specialistProfile.getSpecialistProfiles()
					.size(); i++) {
				specialistProfile.getSpecialistProfiles().get(i)
						.getSpecialityProfile().getTotalCompetence()
						.sortByLevel();
				System.out.println(specialistProfile.getSpecialistProfiles()
						.get(i).getSpecialityProfile().getTotalCompetence()
						.toString());
			}
			System.out
					.println("========== END OF INITIAL PROFILE ==========\n");
			/*
			 * And Specialist Profile with Competencies Levels
			 */
			System.out.println("3 - COMPETENCY VALUES:");
			System.out.println("==============================");
			printCompetencyValues(specialistProfile.getSpecialistProfiles());
			/*
			 * Here we get the Competencies with required Level
			 */
			System.out.println("\n"
					+ "4 - LOW PRIORITY COMPETENCIES (Level < 5%):");
			System.out.println("---------------------------------");
			getPriorityCompetencies(specialistProfile.getSpecialistProfiles(),
					0, LOW_PRIORITY_TO);
			System.out
					.println("5 - MEDIUM PRIORITY COMPETENCIES (5 <= Level < 30 %):");
			System.out.println("---------------------------------");
			getPriorityCompetencies(specialistProfile.getSpecialistProfiles(),
					MEDIUM_PRIORITY_FROM, MEDIUM_PRIORITY_TO);
			System.out
					.println("6 - HIGH PRIORITY COMPETENCIES (30 <= Level <= 100 %):");
			System.out.println("---------------------------------");
			getPriorityCompetencies(specialistProfile.getSpecialistProfiles(),
					HIGH_PRIORITY_FROM, HIGH_PRIORITY_TO);

			System.out.println("---------------------------------");
			System.out.println("7 - TECH COMPETENCIES IN A SE PROFILE:");
			System.out.println("---------------------------------");

			TechCompetency dummyTech = new TechCompetency(1, "Dummy Tech", 0.1);

			CompetenceBoxCreator boxTechSE = new CompetenceBoxCreator(
					specialistProfile.getSpecialistProfiles().get(0)
							.getSpecialityProfile().getTotalCompetence()
							.getCompetenciesOfOneType((dummyTech.getClass())));
			log.info(boxTechSE.getBoxContainer().toString());
			System.out.println("---------------------------------");
			System.out.println("8 - TECH COMPETENCIES IN A TE PROFILE:");
			System.out.println("---------------------------------");
			CompetenceBoxCreator boxTechTE = new CompetenceBoxCreator(
					specialistProfile.getSpecialistProfiles().get(1)
							.getSpecialityProfile().getTotalCompetence()
							.getCompetenciesOfOneType((dummyTech.getClass())));
			log.info(boxTechTE.getBoxContainer().toString());
			System.out.println("---------------------------------");
			System.out.println("9 - COMPARE COMPETENCIES IN SE & TE PROFILES:");
			System.out.println("---------------------------------");
			System.out.println("SE Profile and TE Profile are equal: "
					+ boxTechSE.containsSame(boxTechTE.getBoxInstance()));
			System.out.println("SE Profile is equal to itself: "
					+ boxTechSE.containsSame(boxTechSE.getBoxInstance()));
			log.info("End process");

		} catch (Exception e) {
			log.error(e.getMessage());
		}

	}

}